﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace sdamarketing.SocialNet
{
    public partial class OdberiPrijatel : osnova1
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                try
                {
                    if (Session["iduser"] == null || Session["username"] == null)
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                    else
                    {
                        // AN Go polnam gridot so site korisnici od baza (za da ne bide prazen na start) ova moze i da se izmeni
                        DataSet dataSet = Users.SearchExcludeUser(Int32.Parse(Session["iduser"].ToString()),"","","","");
                        UsersGridView.DataSource = dataSet.Tables["DataSet"];
                        UsersGridView.DataBind();

                        GridView1.DataSource = ReturnDataSet.ReturSet("SELECT UsernamePrijatel FROM Prijatel WHERE (idUser = " + Session["iduser"].ToString() + ")");
                        GridView1.DataBind();
                       
                        Panel1.Visible = false;
                        Button2.Visible = false;
                    }
                }
                catch (Exception exception)
                {
                    SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() );
                }
            }
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            try
            {
                // AN Go polnam gridot spored vnesenite search kriteriumite
                DataSet dataSet = Users.Search(TextBoxName.Text, TextBoxLastName.Text, TextBoxUsername.Text,"");
                UsersGridView.DataSource = dataSet.Tables["DataSet"];
                UsersGridView.DataBind();
            }
            catch (Exception exception)
            {
                SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() );
            }
        }

        protected void UsersGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                UsersGridView.PageIndex = e.NewPageIndex;
                UsersGridView.SelectedIndex = -1;

                DataSet dataSet = Users.Search(TextBoxName.Text, TextBoxLastName.Text, TextBoxUsername.Text, "");
                UsersGridView.DataSource = dataSet.Tables["DataSet"];
                UsersGridView.DataBind();
            }
            catch (Exception exception)
            {
                SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() );
            }           
        }

        protected void UsersGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
                    Users.AddPrijatel(0, Int32.Parse(Session["iduser"].ToString()),Users.FindUser(UsersGridView.SelectedRow.Cells[2].Text),Session["username"].ToString(),UsersGridView.SelectedRow.Cells[2].Text);
                    lblSelectOne.Text = "Корисникот со корисничко име : " + UsersGridView.SelectedRow.Cells[2].Text + " e додаден како ваш пријател";
                    GridView1.DataSource = ReturnDataSet.ReturSet("SELECT UsernamePrijatel FROM Prijatel WHERE (idUser = " + Session["iduser"].ToString() + ")");
                    GridView1.DataBind();
        }
        
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Users.RemovePrijatel(Int32.Parse(Session["iduser"].ToString()), GridView1.Rows[e.RowIndex].Cells[0].Text);
            GridView1.DataSource = ReturnDataSet.ReturSet("SELECT UsernamePrijatel FROM Prijatel WHERE (idUser = " + Session["iduser"].ToString() + ")");
            GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            Button1.Visible = false;
            Button2.Visible = true;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Button2.Visible = false;
            Button1.Visible = true;
        }

    }
}
